<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;

class Upgrade extends Command {

    protected $signature = 'st:upgrade';
    protected $description = '咨询部学员年级升级';

    public function __construct(){
        parent::__construct();
    }
	/**
	 * [判断处理方式]
	 */
    public function handle() {
		DB::beginTransaction();
		try {
			$st = DB::table('consultation_st')->get(['id','school_info']);
			$st = json_decode(json_encode($st),true);
			$bar = $this->output->createProgressBar(sizeof($st));
			foreach($st as $s){
				$info = json_decode($s['school_info'],true);
				if($info['stage'] > 0) {
					if($info['grade'] < 13){
						if($info['special'] == "1" && $info['grade'] == "5"){
							$info['grade'] += 2;
						}else{
							$info['grade'] += 1;
						}
					}
					if($info['grade'] > 0 && $info['grade'] < 7) {
						$info['stage'] = "1";
					}elseif($info['grade'] > 6 && $info['grade'] < 10){
						$info['stage'] = "2";
					}elseif($info['grade'] > 9 && $info['grade'] < 13){
						$info['stage'] = "3";
					}else{
						$info['stage'] = "4";
					}
				}
				foreach($info as &$i){
					$i = (string)$i;
				}
				DB::table('consultation_st')->where("id","=",$s['id'])->update([
					'school_info' => json_encode($info)
				]);
				$bar->advance();
			}
			$bar->finish();
			DB::commit();
			echo "\n Success\n";
		} catch (\Exception $e) {
			DB::rollback();
			echo "\n Failed:".$e->getMessage()."\n";
		}
    }
}
